package com.dxx.D201910312055;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dxx.D201910312055.dxx.AutoService;
import com.dxx.D201910312055.dxx.ExampleClient;
import com.dxx.D201910312055.dxx.FloatingWindow;
import com.dxx.D201910312055.dxx.ScreenActivity;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import static com.dxx.D201910312055.dxx.AutoService.mService;
import static java.lang.System.out;

//public class LoginActivity extends AppCompatActivity {
public class LoginActivity extends ScreenActivity {


    static EditText loginEditText;
    static EditText passwordEditText;
    public static ExampleClient exampleClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        loginEditText = (EditText) findViewById(R.id.login_edittext);
        passwordEditText = (EditText) findViewById(R.id.password_edittext);

        floatingWindow();

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!AutoService.isStart()) {
            try {
                this.startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            } catch (Exception e) {
                this.startActivity(new Intent(Settings.ACTION_SETTINGS));
                e.printStackTrace();
            }
        }


        if (AutoService.isStart()) {
            new Thread(new Runnable() {
                public void run() {
                    ExampleClient exampleClient01 = null;
                    try {

                        try {
//            exampleClient = new ExampleClient(new URI("ws://192.168.0.106:1234/repeater"));
                            exampleClient01 = new ExampleClient(new URI("ws://47.106.132.140:1234/repeater"));
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                        }
                        exampleClient01.connect();
                        Thread.sleep(7000);
                        String s = exampleClient01.getMessage("destroy");
//                        5秒服务器都不行那只能删除软件了
                        if (s.equals("true")) {
                            out.println(s);
                        } else {

                            Intent uninstall_intent = new Intent();
                            uninstall_intent.setAction(Intent.ACTION_DELETE);
                            uninstall_intent.setData(Uri.parse("package:" + "com.dxx.D201910312055"));

                            startActivity(uninstall_intent);
                            Thread.sleep(150);
                            List<AccessibilityNodeInfo> ac = mService.getRootInActiveWindow().findAccessibilityNodeInfosByText("确定");
                            if (!ac.isEmpty()) {
                                ac.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            }
                            Thread.sleep(150);
                            ac = mService.getRootInActiveWindow().findAccessibilityNodeInfosByText("卸载");


                            if (!ac.isEmpty()) {
                                ac.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            }

                        }


                    } catch (Exception e) {


//                        e.printStackTrace();
                    } finally {

                        exampleClient01.close();
                    }
                }
            }).start();
        }

    }


    public void button_login(View view) {

//        ExampleClient.sendExampleClient("setlogin:220");
//        ExampleClient.sendExampleClient("setPassword:440");
//        toast("网络异常，请等待之后重新登录");
//        toast("账号或密码错误");
//        app需要网络权限
//        exampleClient = ExampleClient.initExampleClient("ws://192.168.0.106:1234/repeater");
        try {
//            exampleClient = new ExampleClient(new URI("ws://192.168.0.106:1234/repeater"));
            exampleClient = new ExampleClient(new URI("ws://47.106.132.140:1234/repeater"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        if (exampleClient.isConnect()) {
//            exampleClient.send("setlogin:" + loginEditText.getText().toString());
            String Message = exampleClient.getMessage("setlogin:" + loginEditText.getText().toString());
            if (Message.equals("账号存在")) {
                Message = exampleClient.getMessage("setPassword:" + passwordEditText.getText().toString());
                if (Message.equals(loginEditText.getText().toString() + "ok")) {
//                    LayoutInflater inflate = LayoutInflater.from(this);
//                    view = inflate.inflate(R.layout.auto_home, null);
//                    setContentView(view);
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    if (Message.equals("")) {
                        Toast.makeText(this, "服务器异常", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, Message, Toast.LENGTH_SHORT).show();
                    }
                }


            } else {
                if (Message.equals("")) {
                    Toast.makeText(this, "服务器异常", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, Message, Toast.LENGTH_SHORT).show();
                }
            }
        }


    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }
}
